package com.jeequan.jeepay.mch.aop;

import com.jeequan.jeepay.core.entity.PayOrderDO;
import com.jeequan.jeepay.core.model.security.JeeUserDetails;
import com.jeequan.jeepay.core.utils.LogUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Aspect
@Component
public class PayOrderFilterAspect {

//    @Autowired
//    JadeReportDbFieldDao jadeReportDbFieldDao;
//    @Autowired
//    JadeDictItemDao jadeDictItemDao;
    /**
     * 切入点
     */
    @Pointcut("execution(public * com.jeequan.jeepay.mch.controller.PayOrderController.paginQuery(..))")
    public void pointCut() {

    }

    /**
     * 字典键值匹配
     *
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around("pointCut()")
    public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
        // 1.获取类的字节码对象，通过字节码对象获取方法信息
        Class<?> targetCls = pjp.getTarget().getClass();
        // 2.获取方法签名(通过此签名获取目标方法信息)
        MethodSignature ms = (MethodSignature) pjp.getSignature();
        // 3.获取目标方法上的注解指定的操作名称
        Method targetMethod = targetCls.getDeclaredMethod(ms.getName(), ms.getParameterTypes());
        // 4.获取目标方法名(目标类型+方法名)
        String targetClsName = targetCls.getName();
        String targetObjectMethodName = targetClsName + "." + ms.getName();
        // 5.获取请求参数
        Object[] args = pjp.getArgs();
        String targetMethodParams = Arrays.toString(args);
        PayOrderDO payOrderDO = (PayOrderDO)args[0];
        ///当前用户信息
        JeeUserDetails jeeUserDetails = JeeUserDetails.getCurrentUserDetails();
        payOrderDO.setMemberId(jeeUserDetails.getSysUser().getId().intValue());
        args[0] = payOrderDO;
        // 6.执行请求，并打印结果
        Object result = pjp.proceed(args);
        // 7.打印日志
        LogUtils.op("PayOrderFilterAspect.aroundAdvice").msg("查询订单").kv("method", targetObjectMethodName)
                .kv("params", targetMethodParams).kv("result", result).info();
        return result;
    }

}
